package cn.walls1717.juc.future;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * FutureTask的测试
 *
 * @author walls1717
 * @create 2022/12/08 20:15
 **/
@Slf4j
public class FutureTaskDemo {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        FutureTask<String> futureTask = new FutureTask<>(new MyThread());
        Thread t1 = new Thread(futureTask, "t1");
        t1.start();

        // 通过FutureTask可以获得线程执行的结果
        String result = futureTask.get();
        log.info("futureTask result -> {}", result);

    }

}

@Slf4j
class MyThread implements Callable<String> {

    @Override
    public String call() throws Exception {
        log.info("call()被调用");
        return "hello callable";
    }
}
